package com.fr.report.script.function;

import com.fr.base.FRContext;
import com.fr.data.TableDataException;
import com.fr.data.impl.DBTableData;
import com.fr.data.impl.NameDatabaseConnection;
import com.fr.data.impl.TableDataAdapter;
import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import com.fr.report.script.core.FArray;
import com.fr.util.Utils;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SQL extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    Object localObject2;
    if (paramArrayOfObject.length < 3)
      return Primitive.ERROR_NAME;
    String str1 = Utils.objectToString(paramArrayOfObject[0]);
    String str2 = Utils.objectToString(paramArrayOfObject[1]);
    int i = Utils.objectToNumber(paramArrayOfObject[2], false).intValue() - 1;
    int j = -1;
    if (paramArrayOfObject.length > 3)
      j = Utils.objectToNumber(paramArrayOfObject[3], false).intValue() - 1;
    int k = -1;
    if (paramArrayOfObject.length > 4)
      k = Utils.objectToNumber(paramArrayOfObject[4], false).intValue();
    TableDataAdapter localTableDataAdapter = DBTableData.createCacheableAdapter(new NameDatabaseConnection(str1), str2, 0, k);
    try
    {
      if (j >= 0)
      {
        localObject1 = localTableDataAdapter.getValueAt(j, i);
        jsr 137;
        return localObject1;
      }
      Object localObject1 = new Object[localTableDataAdapter.getRowCount()];
      for (int l = 0; l < localObject1.length; ++l)
        localObject1[l] = localTableDataAdapter.getValueAt(l, i);
      if (localObject1.length == 1)
      {
        localObject2 = localObject1[0];
        jsr 77;
        return localObject2;
      }
      if (localObject1.length == 0)
      {
        localObject2 = Primitive.NULL;
        jsr 60;
        return localObject2;
      }
      localObject2 = new FArray(localObject1);
      return localObject2;
    }
    catch (TableDataException localTableDataException)
    {
      FRContext.getLogger().log(Level.WARNING, localTableDataException.getMessage(), localTableDataException);
      localObject2 = Primitive.NULL;
      return localObject2;
    }
    finally
    {
      try
      {
        localTableDataAdapter.tryDestroy();
      }
      catch (SQLException localSQLException)
      {
        FRContext.getLogger().log(Level.WARNING, localSQLException.getMessage(), localSQLException);
      }
    }
  }

  public Function.Type getType()
  {
    return Function.REPORT;
  }

  public String getCN()
  {
    return "SQL(connectionName,sql,columnIndex,rowIndex)返回通过sql语句从connectionName中获得数据表的第columnIndex列第rowIndex行所对应的元素。\nconnectionName：数据库库的名字，字符串形式；\nsql:SQL语句，字符串形式；\ncolumnIndex:列序号，整形;\nrowIndex:行序号，整形。\n备注:行序号可以不写，这样返回值为数据列。\n示例：\n以我们提供的数据源HSQL为例\nSQL(\"HSQL\",\"SELECT * FROM CUSTOMER\",2,2)等于王先生。";
  }

  public String getEN()
  {
    return "SQL(connectionName,sql,columnIndex,rowIndex)returns the content at column columnIdex and row rowIndex form the table, which is selected from the connection, buy the sql Statement. \nsql:SQL Statement，string type；\ncolumnIndex:index of column，int type;\nrowIndex:index of row，int type。\nRemarks:\nThe parameter of rowIndex is non-essential, and when it's absent a field will be returned.\nExamples:\nData source HSQL，for instance\nSQL(\"HSQL\",\"SELECT * FROM CUSTOMER\",2,2)returns \"Mr Wang\"。";
  }
}